package org.cboard.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.cboard.dto.User;
import org.cboard.pojo.DashboardBoard;
import org.cboard.security.service.DbUserDetailService;
import org.cboard.security.utils.ErrorCodeEnum;
import org.cboard.security.utils.MD5Util;
import org.cboard.services.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by james_duan on 2017/2/16.
 */
@RestController
@RequestMapping(value = "/thirdParty")
public class ThirdPartyApiController {

    @Autowired
    private BoardService boardService;
    @Autowired
    private DbUserDetailService dbUserDetailService;

    @RequestMapping("/availablePublicUrls")
    public JSONObject getAvailablePublishUrlsByUser(@RequestParam String username, @RequestParam String password,
                                                    HttpServletRequest request) {
        JSONObject json = new JSONObject();
        try {
            if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
                User user = (User) dbUserDetailService.loadUserByUsername(username);
                boolean isPassed = MD5Util.verifyingUserDetailByNameAndPassword(username, password, user);
                // 校验用户名密码
                if (isPassed) {
                    json.put("code", ErrorCodeEnum.SUCCESSFUL.getCode());
                    json.put("msg", ErrorCodeEnum.SUCCESSFUL.getMessage());
                    //获取对应的发布的Url
                    List<DashboardBoard> boardList = boardService.getBoardList(user.getUserId());
                    //过滤可用的publishUrl状态
                    JSONArray jsonArray = new JSONArray();
                    for (DashboardBoard dashboardBoard : boardList) {
                        if (dashboardBoard.getPublishStatus() == 1 && !StringUtils.isEmpty(dashboardBoard.getPublishUrl())) {
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put("boardName", dashboardBoard.getName());
                            jsonObject.put("publishUrl", dashboardBoard.getPublishUrl());
                            jsonArray.add(jsonObject);
                        }
                    }
                    json.put("data", jsonArray);
                    return json;
                }
            }

        } catch (Exception ex) {
            json.put("code", ErrorCodeEnum.SERVER_ERROR.getCode());
            json.put("msg", ErrorCodeEnum.SERVER_ERROR.getMessage());
            return json;
        }

        json.put("code", ErrorCodeEnum.USER_NOT_EXIST_OR_PASSWORD_ERROR.getCode());
        json.put("msg", ErrorCodeEnum.USER_NOT_EXIST_OR_PASSWORD_ERROR.getMessage());
        return json;
    }
}
